package com.yiyoucheng.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yiyoucheng.entity.PageResult;
import com.yiyoucheng.entity.QueryPageBean;
import com.yiyoucheng.mapper.EmployeeMapper;
import com.yiyoucheng.pojo.Employee;
import com.yiyoucheng.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    //查询所有员工
    @Override
    public List<Employee> findAll() {
        return employeeMapper.findAll();
    }

    /*//分页查询员工信息
    @Override
    public PageResult findPage(String queryString,int currentPage,int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        Page<Employee> page = employeeMapper.selectByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }*/

    //员工信息条件+分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Map> page = employeeMapper.selectByCondition(queryPageBean.getQueryCondition());
        return new PageResult(page.getTotal(),page.getResult());
    }

    //根据id查询员工信息
    @Override
    public Employee findById(Integer id) {
        return employeeMapper.findById(id);
    }

    //新增员工信息
    @Override
    public void add(Employee employee) {
        //新增员工状态status设置为1
        employee.setStatus(1);
        employeeMapper.add(employee);
    }

    //更改员工信息
    @Override
    public void update(Employee employee) {
        employeeMapper.update(employee);
    }

    //根据id删除员工信息(软删除)
    @Override
    public void deleteById(Integer id) {
        employeeMapper.deleteById(id);
    }

    //根据员工编号查询员工信息
    @Override
    public Employee findByCode(String code) {
        return employeeMapper.findByCode(code);
    }

    //查询所有员工的地址信息
    @Override
    public List<Map> findAddressList() {
        //从数据库查出来的值,可能含有重复的值
        List<String> beforeList = employeeMapper.findAddressList();
        //存储去重后的值的集合
        List<String> addressList = new ArrayList<>();
        for (String address : beforeList) {
            //当集合中不包含这个值时,将其添加进集合里
            if(!addressList.contains(address)){
                addressList.add(address);
            }
        }
        List<Map> addressMapList = new ArrayList<>();
        for (String address : addressList) {
            Map addressMap = new HashMap();
            addressMap.put("value",address);
            addressMapList.add(addressMap);
        }
        return addressMapList;
    }

    //查询所有员工的姓名信息
    @Override
    public List<Map> findNameList() {
        //从数据库查出来的值,可能含有重复的值
        List<String> beforeList = employeeMapper.findNameList();
        //存储去重后的值的集合
        List<String> nameList = new ArrayList<>();
        for (String name : beforeList) {
            //当集合中不包含这个值时,将其添加进集合里
            if(!nameList.contains(name)){
                nameList.add(name);
            }
        }
        List<Map> nameMapList = new ArrayList<>();
        for (String name : nameList) {
            Map nameMap = new HashMap();
            nameMap.put("value",name);
            nameMapList.add(nameMap);
        }
        return nameMapList;
    }
}
